package com.alibaba.fastjson2.jsonpath;

import com.alibaba.fastjson2.JSONPath;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;

public class JSONPath_between_double {
    @Test
    public void test_between() throws Exception {
        List list = new ArrayList();
        list.add(new Entity(101, "kiki"));
        list.add(new Entity(102, "ljw2083"));
        list.add(new Entity(103, "ljw2083"));
        List<Object> result = (List<Object>) JSONPath.eval(list, "$[?(@.id between 101 and 101)]");
        assertEquals(1, result.size());
        assertSame(list.get(0), result.get(0));
    }

    @Test
    public void test_between_2() throws Exception {
        List list = new ArrayList();
        list.add(new Entity(101, "kiki"));
        list.add(new Entity(102, "ljw2083"));
        list.add(new Entity(103, "ljw2083"));
        List<Object> result = (List<Object>) JSONPath.eval(list, "$[?(@.id between 101 and 102)]");
        assertEquals(2, result.size());
        assertSame(list.get(0), result.get(0));
        assertSame(list.get(1), result.get(1));
    }

    @Test
    public void test_between_not() throws Exception {
        List list = new ArrayList();
        list.add(new Entity(101, "kiki"));
        list.add(new Entity(102, "ljw2083"));
        list.add(new Entity(103, "ljw2083"));
        List<Object> result = (List<Object>) JSONPath.eval(list, "$[?(@.id not between 101 and 102)]");
        assertEquals(1, result.size());
        assertSame(list.get(2), result.get(0));
    }

    public static class Entity {
        private Double id;
        private String name;

        public Entity(int id, String name) {
            this.id = Double.valueOf(id);
            this.name = name;
        }

        public Double getId() {
            return id;
        }

        public void setId(Double id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}
